package com.tedu.egmvc.controller;


import com.tedu.egmvc.mapper.AddressMapper;
import com.tedu.egmvc.pojo.dto.AddressDTO;
import com.tedu.egmvc.pojo.entity.Address;
import com.tedu.egmvc.pojo.vo.AddressVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

//@Controller
@RestController
@RequestMapping("/address")
@Api(tags = "收货地址模块")
public class AddressController {

    @Autowired
    private AddressMapper addressMapper;

    // 添加收货地址
    @ApiOperation("添加收货地址功能")
    @PostMapping("/insert")
    //@ResponseBody
    public String insertAddress(@RequestBody AddressDTO addressDTO){
        Address address=new Address();
        BeanUtils.copyProperties(addressDTO,address);
        int num=addressMapper.insertAddress(address);
        return num>0?"添加成功":"添加失败";
    }


    // 查询收货地址
    @ApiOperation("查询所有收货地址功能")
    @GetMapping("/select")
    //@ResponseBody
    public List<AddressVO> selectAddress(){
        return addressMapper.selectAddress();
    }

    // 删除收货地址
    @ApiOperation("删除收货地址功能")
    @DeleteMapping("/delete/{id}")
    @ApiImplicitParam(name = "id",value = "地址id",required = true,dataType = "int",example = "1")
    //@ResponseBody
    public String deleteAddress(@PathVariable int id){

        int num=addressMapper.deleteAddress(id);
        return num>0? "删除成功":"删除失败";
    }

    // 修改收货地址
    @ApiOperation("修改收货地址功能")
    @PutMapping("/update")
    //@ResponseBody
    public String updateAddress(@RequestBody Address address){
        System.out.println("测试输出:"+address);
        int num=addressMapper.updateAddress(address);
        return num>0?"修改成功":"修改失败";
    }


}
